home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997…eptember: Technology Seed / ATS Aug-Sept '97.toast / Navigation Services SDK / Examples / SimpleText / SimpleText ƒ / NavigationServicesSupport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  7.2 KB  |  333 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        NavigationServicesSupport.c
  3.  
  4.     Contains:    <contents>
  5.  
  6.     Written by:    Sean Findley
  7.  
  8.     Copyright:    <copyright>
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <17>    /19/1997    Yan        Mixed mode support
  13.         <15>     5/23/97    sjf     
  14. */
  15.  
  16.  
  17. /*
  18.     File:        NavigationServicesSupport.c
  19.  
  20.     Contains:    xxx put contents here xxx
  21.  
  22.     Version:    xxx put the technology version here xxx
  23.  
  24.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  25.  
  26.     File Ownership:
  27.  
  28.         DRI:                Yan Arrouye
  29.  
  30.         Other Contact:        xxx put alternate contact (owner’s functional manager) here xxx
  31.  
  32.         Technology:            xxx put the technology group name here xxx
  33.  
  34.     Writers:
  35.  
  36.         (KLM)    Keith Mortensen
  37.         (Yan)    Yan Arrouye
  38.  
  39.     Change History (most recent first):
  40.  
  41.         <14>     5/15/97    Yan        Updated for new API
  42.         <13>     5/08/97    KLM        Fixed NewOpenHandle bug, it was never creating a open rsrc.
  43.         <12>     5/08/97    Yan        Use isStationery flag
  44.         <11>     4/21/97    Yan        
  45.         <10>     4/21/97    Yan        NavGetDefaultDialogsOptions really
  46.          <9>     4/21/97    Yan        Use new NavSetDefaultDialogsOptions
  47.          <8>     4/21/97    Yan        Implemented isStationery in save
  48.          <7>     4/21/97    Yan        Updated for latest include
  49.          <6>     4/18/97    Yan        Rev'ed for new API
  50.          <5>     4/14/97    Yan        Added a NULL version of this file for classic 68K runtime (not
  51.                                     supported)
  52. */
  53.  
  54. #include "NavigationServicesSupport.h"
  55.  
  56.  
  57.  
  58. #include <CodeFragments.h>
  59. #include <Finder.h>
  60. #include <Dialogs.h>
  61. #include <LowMem.h>
  62. #include <string.h>
  63.  
  64.  
  65.  
  66.  
  67.  
  68. static Handle NewOpenHandle(OSType applicationSignature, short numTypes, OSType typeList[])
  69. {
  70.     Handle hdl = NULL;
  71.     
  72.     if ( numTypes > 0 )
  73.     {
  74.     
  75.         hdl = NewHandle(sizeof(OpenResource) + numTypes * sizeof(OSType));
  76.     
  77.         if ( hdl != NULL )
  78.         {
  79.             OpenResourceHandle open        = (OpenResourceHandle)hdl;
  80.             
  81.             (*open)->componentSignature = applicationSignature;
  82.             (*open)->osTypeCount        = numTypes;
  83.             BlockMoveData(typeList, (*open)->osType, numTypes * sizeof(OSType));
  84.         }
  85.     }
  86.     
  87.     return hdl;
  88. }
  89.  
  90.  
  91.  
  92.  
  93. static OSStatus SendOpenAE(AEDescList list)
  94. {
  95.     OSStatus        err;
  96.     AEAddressDesc    theAddress;
  97.     AppleEvent        dummyReply;
  98.     AppleEvent        theEvent;
  99.     
  100.     theAddress.descriptorType    = typeNull;
  101.     theAddress.dataHandle        = NULL;
  102.  
  103.     do {
  104.         ProcessSerialNumber psn;
  105.     
  106.         err = GetCurrentProcess(&psn);
  107.         if ( err != noErr) break;
  108.         
  109.         err =AECreateDesc(typeProcessSerialNumber, &psn, sizeof(ProcessSerialNumber), &theAddress);
  110.         if ( err != noErr) break;
  111.             
  112.         dummyReply.descriptorType    = typeNull;
  113.         dummyReply.dataHandle        = NULL;
  114.  
  115.         err = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &theAddress, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
  116.         if ( err != noErr) break;
  117.         
  118.         err = AEPutParamDesc(&theEvent, keyDirectObject, &list);
  119.         if ( err != noErr) break;
  120.         
  121.         err = AESend(&theEvent, &dummyReply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
  122.         if ( err != noErr) break;
  123.         
  124.             
  125.     } while (false);
  126.     
  127.     return err;
  128. }
  129.  
  130.  
  131.  
  132. OSStatus OpenFileDialog(OSType applicationSignature, short numTypes, OSType typeList[], NavEventProcPtr eventProc, FSSpec* fileSpec, OSType* fileType)
  133. {
  134.     NavReplyRecord            theReply;
  135.     NavDialogOptions        dialogOptions;
  136.     OSErr                    theErr            = noErr;
  137.     Handle                    openHandle        = NULL;
  138.     NavEventProcUPP            eventProcUPP    = NewNavEventProc(eventProc);
  139.  
  140.     NavGetDefaultDialogOptions(&dialogOptions);
  141.  
  142.     openHandle = NewOpenHandle(applicationSignature, numTypes, typeList);
  143.     if ( openHandle )
  144.     {
  145.         HLock(openHandle);
  146.     }
  147.     
  148.     theErr = NavGetFile(NULL, &theReply, &dialogOptions, eventProcUPP, NULL, NULL, openHandle, NULL);
  149.     DisposeRoutineDescriptor(eventProcUPP);
  150.     
  151.     if (theReply.validRecord)
  152.     {
  153.         // grab the target FSSpec from the AEDesc for opening:    
  154.         AEDesc     resultDesc;
  155.         FInfo    fileInfo;
  156.  
  157.         // Is this a single file open
  158.         if ( fileSpec != NULL )
  159.         {
  160.             AEKeyword keyword;
  161.             
  162.             theErr = AEGetNthDesc(&theReply.selection, 1, typeFSS, &keyword, &resultDesc);
  163.             if (theErr == noErr)
  164.                 BlockMove(*resultDesc.dataHandle,fileSpec,sizeof(FSSpec));
  165.                 
  166.             // decide if the doc we fileSpec opening is a PICT or TEXT
  167.             theErr = FSpGetFInfo(fileSpec, &fileInfo);
  168.             if (theErr == noErr)
  169.             {
  170.                 if ( fileType != NULL )
  171.                     *fileType = fileInfo.fdType;
  172.             }
  173.         }
  174.         else
  175.         {
  176.             // Multiple files open: use ApleEvents
  177.             theErr = SendOpenAE(theReply.selection);
  178.         }
  179.  
  180.         NavDisposeReply(&theReply);
  181.     }
  182.     else
  183.     {
  184.         theErr = userCanceledErr;
  185.     }
  186.  
  187.     if (openHandle != NULL)
  188.     {
  189.         HUnlock(openHandle);
  190.         DisposeHandle(openHandle);
  191.     }
  192.     
  193.     return theErr;
  194. }
  195.  
  196.  
  197. short ConfirmSaveDialog(StringPtr documentName, Boolean quitting, NavEventProcPtr eventProc)
  198. {
  199.     OSStatus                theStatusErr     = noErr;
  200.     OSErr                     theErr             = noErr;
  201.     NavAskSaveChangesResult    reply             = 0;
  202.     NavAskSaveChangesAction    action             = 0;
  203.     NavEventProcUPP            eventProcUPP    = NewNavEventProc(eventProc);
  204.     short                    result;
  205.     
  206.     if (quitting)
  207.         action = kNavSaveChangesQuittingApplication;
  208.     else
  209.         action = kNavSaveChangesClosingDocument;
  210.         
  211.     theErr = NavAskSaveChanges(    LMGetCurApName(),
  212.                                 documentName,
  213.                                 action,
  214.                                 &reply,
  215.                                 eventProcUPP,
  216.                                 NULL);
  217.     DisposeRoutineDescriptor(eventProcUPP);
  218.     
  219.     // Map reply code to ok, cancel, dontSave
  220.     switch (reply)
  221.     {
  222.         case askSaveChangesSave:
  223.             result = ok;
  224.             break;
  225.             
  226.         case askSaveChangesCancel:
  227.             result = cancel;
  228.             break;
  229.             
  230.         case askSaveChangesDontSave:
  231.             result = dontSaveChanges;
  232.             break;
  233.     }
  234.     
  235.     return result;
  236. }
  237.  
  238.  
  239.  
  240. OSStatus SaveFileDialog(StringPtr fileName, OSType filetype, OSType fileCreator, 
  241.                         NavEventProcPtr eventProc, FSSpec* fileSpec, 
  242.                         Boolean* stationery, Boolean* replacing, NavReplyRecord* reply)
  243. {
  244.     NavDialogOptions    dialogOptions;
  245.     OSErr                theErr            = noErr;
  246.     NavEventProcUPP        eventProcUPP    = NewNavEventProc(eventProc);
  247.  
  248.     NavGetDefaultDialogOptions(&dialogOptions);
  249.  
  250.     dialogOptions.dialogOptionFlags |= (stationery != NULL ? kAllowStationery : 0);
  251.     BlockMoveData(fileName, (unsigned char*)&dialogOptions.savedFileName, fileName[0] + 1);
  252.  
  253.     theErr = NavPutFile(NULL, reply, &dialogOptions, eventProcUPP, NULL, filetype, fileCreator);
  254.     DisposeRoutineDescriptor(eventProcUPP);
  255.     
  256.     if (reply->validRecord)
  257.     {
  258.         // User saved
  259.         AEDesc     resultDesc;
  260.         AEKeyword keyword;
  261.             
  262.         // retrieve the returned selection:
  263.         theErr = AEGetNthDesc(&reply->selection, 1, typeFSS, &keyword, &resultDesc);
  264.         if (theErr == noErr)
  265.             BlockMove(*resultDesc.dataHandle, fileSpec, sizeof(FSSpec));
  266.  
  267.         if ( replacing != NULL )
  268.             *replacing = reply->replacing;
  269.         
  270.         if ( stationery != NULL )
  271.         {
  272.             *stationery    = reply->isStationery;
  273.         }
  274.     }
  275.     else
  276.     {
  277.         // User cancelled
  278.         if ( replacing != NULL )
  279.             *replacing = false;
  280.         
  281.         if ( stationery != NULL )
  282.             *stationery    = false;    
  283.  
  284.         theErr = userCanceledErr;
  285.     }
  286.     
  287.     return theErr;
  288. }
  289.  
  290.  
  291.  
  292. OSStatus CompleteSave(const FSSpec*, NavReplyRecord* reply)
  293. {
  294.     OSStatus theErr;
  295.     
  296.     if (reply->validRecord)
  297.     {
  298.         theErr = NavCompleteSave(reply, kNavTranslateInPlace);
  299.     }
  300.  
  301.     theErr = NavDisposeReply(reply);
  302.     
  303.     return theErr;
  304. }
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312. //
  313. // Callback to handle events that occur while navigation dialogs are up but really should be handled by the application
  314. //
  315.  
  316. extern void HandleEvent(EventRecord * pEvent);
  317.  
  318.  
  319. pascal void MyEventProc(const NavEventCallbackMessage callBackSelector, 
  320.                         NavCBRecPtr callBackParms, 
  321.                         NavCallBackUserData /*callBackUD*/)
  322. // Callback to handle event passing betwwn the navigation dialogs and the applicatio
  323. {
  324.     if ( callBackSelector == kNavCBEvent )
  325.         switch (callBackParms->u.event->what)
  326.         {
  327.             case updateEvt:
  328.                 HandleEvent(callBackParms->u.event);
  329.                 break;
  330.         }
  331. }
  332.  
  333.